﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MayhemCore;
using MayhemCore.ModuleTypes;
using System.Diagnostics;
using System.Windows.Threading;

namespace MayhemWP7.Modules.Reactions
{
    [MayhemModule("Mayhem Desktop Event", "Triggers the WP7 Reaction Event on a paired desktop")]
    public class MayhemEvent : ReactionBase, IWp7Configurable
    {
        #region Configuration Properties
        private string _ip;
        public string IP
        {
            get
            {
                return _ip;
            }
            set
            {
                _ip = value;
                SetConfigString();
            }
        }

        private string _port;
        public string Port
        {
            get
            {
                return _port;
            }
            set
            {
                _port = value;
                SetConfigString();
            }
        }

        private string _guid;
        public string Guid
        {
            get
            {
                return _guid;
            }
            set
            {
                _guid = value;
                SetConfigString();
            }
        }
        #endregion

        protected override void Initialize()
        {
            IP = "192.168.1.134";
            Port = "19283";
            Guid = "e54421c5-fd8e-46fe-81f9-baca876bbe62";
        }

        #region Configuration Views

        public Uri Wp7Config()
        {
            return new Uri("/Modules/Pages/MayhemEventConfig.xaml", UriKind.RelativeOrAbsolute);
        }

        #endregion

        public override void SetConfigString()
        {
            this.ConfigString = String.Format("{0}:{1}/{2}", IP, Port, Guid);
        }

        public override void Perform()
        {
            WebClient webClient = new WebClient();
            Uri service = new Uri("http://" + IP + ":" + Port + "/Mayhem/Event/" + Guid+"?time="+DateTime.Now.ToFileTime());
            Debug.WriteLine(service.OriginalString);
            try
            {
                webClient.OpenReadAsync(service);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);   
            }

            //webBrowser1.Navigate(new Uri("http://" + SavedData.IP + ":" + SavedData.Port + "/Mayhem/Html?t=" + DateTime.Now.Ticks));
        }
    }
}
